using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
namespace MicroMsg.UI.Page
{
	public class QQMailNewMailView : BasePage
	{
		private const string TAG = "QQMailNewMailView";
		private string autoAuthUrl = "http://w.mail.qq.com/cgi-bin/login?fun=mautologin&mpwd={0}@{1}@4&fwd=weixin";
		private string writeMailUrl = "http://w.mail.qq.com/cgi-bin/readtemplate?sid={0}&t=compose&from=today&loc=today,,,10";
		private string strCurrUrl;
		private string strOldUrl = "";
		private EventWatcher m_WatcherAutoAuthSuccess;
		private Popup popUp;
		private bool isLoadComplete;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal Grid webview;
		internal WebBrowser newMailDetail;
		internal Canvas canvasProgressBar;
		internal PerformanceProgressBar ProgressBar;
		private bool _contentLoaded;
		public QQMailNewMailView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.PhoneApplicationPage_Loaded));
			this.popUp = new Popup();
		}
		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("QQMailNewMailView", "OnPageIn start");
			base.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.newMailDetailInfoView_BackKeyPress));
			this.isLoadComplete = false;
			SIDInfo sIDInfo = ConfigMgr.read<SIDInfo>();
			if (sIDInfo == null)
			{
				sIDInfo = new SIDInfo();
				sIDInfo.lastUpdateTime = 0.0;
				sIDInfo.strSid = "";
			}
			Account curAccount = AccountMgr.getCurAccount();
			if (curAccount == null)
			{
				Log.smethod_1("QQMailNewMailView", "AccountMgr.getCurAccount() == null");
				return;
			}
			double nowMilliseconds = Util.getNowMilliseconds();
			double num = nowMilliseconds - sIDInfo.lastUpdateTime;
			if (!string.IsNullOrEmpty(sIDInfo.strSid) && num <= 3600000.0)
			{
				string text = string.Format(this.writeMailUrl, sIDInfo.strSid);
				Uri uri = new Uri(text);
				this.newMailDetail.Navigate(uri);
				return;
			}
			string strFSURL = curAccount.strFSURL;
			if (string.IsNullOrEmpty(strFSURL))
			{
				Log.smethod_1("QQMailNewMailView", "AccountMgr.getCurAccount().strFSURL == null");
				this.newMailDetail.Navigate(new Uri("http://w.mail.qq.com/cgi-bin/loginpage?s=session_timeout&f=xhtml&autologin=n&uin=&aliastype=&from=today"));
				return;
			}
			this.newMailDetail.Navigate(new Uri(strFSURL));
		}
		private void web_loadedComplete(object sender, NavigationEventArgs e)
		{
			this.isLoadComplete = true;
			if (this.ProgressBar.IsIndeterminate)
			{
				this.ProgressBar.IsIndeterminate = false;
			}
		}
		private void web_Navigated(object sender, NavigationEventArgs e)
		{
			this.ProgressBar.IsIndeterminate = true;
			string originalString = e.get_Uri().get_OriginalString();
			string text = this.newMailDetail.SaveToString();
			this.strCurrUrl = originalString;
			if (originalString.Contains("mail.qq.com/cgi-bin/folderlist?"))
			{
				string text2 = QQMailNewMailView.updateSid(originalString);
				string text3 = string.Format(this.writeMailUrl, text2);
				Uri uri = new Uri(text3);
				this.newMailDetail.Navigate(uri);
				return;
			}
			if (originalString.Contains("mail.qq.com/cgi-bin/loginpage?s=session_timeout"))
			{
				if (new Sender().sendAutoAuthPack())
				{
					this.m_WatcherAutoAuthSuccess = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerAutoAuthSuccess));
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_AUTOAUTH_SUCCESS, this.m_WatcherAutoAuthSuccess);
					EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_AUTOAUTH_ERR, this.m_WatcherAutoAuthSuccess);
					return;
				}
			}
			else
			{
				if (originalString.Contains("mail.qq.com/cgi-bin/cgi_redirect") && text.ToUpper().Contains("<HTML><HEAD></HEAD><BODY></BODY></HTML>"))
				{
					string chatValidError = strings.ChatValidError;
					MessageBoxManager.ShowToast(chatValidError, 5000, null, true);
				}
			}
		}
		private void onEventHandlerAutoAuthSuccess(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_AUTOAUTH_SUCCESS, this.m_WatcherAutoAuthSuccess);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_AUTOAUTH_ERR, this.m_WatcherAutoAuthSuccess);
			this.m_WatcherAutoAuthSuccess = null;
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_AUTOAUTH_SUCCESS)
			{
				AuthResponse authResponse = (AuthResponse)evtArgs.mObject;
				if (!string.IsNullOrEmpty(authResponse.FSURL))
				{
					this.newMailDetail.Navigate(new Uri(authResponse.FSURL));
					return;
				}
				Log.smethod_1("QQMailNewMailView", "resp.FSURL == null");
			}
		}
		private void newMailDetailInfoView_BackKeyPress(object sender, CancelEventArgs e)
		{
		}
		private void web_scriptNotify(object sender, NotifyEventArgs e)
		{
			e.get_Value().ToString();
		}
		protected override void OnPageOut()
		{
		}
		public static string updateSid(string strAddr)
		{
			if (!strAddr.Contains("sid="))
			{
				return null;
			}
			int num = strAddr.IndexOf("sid=");
			if (num < 0)
			{
				return null;
			}
			string text = strAddr.Substring(num + 4);
			num = text.IndexOf("&");
			if (num < 0)
			{
				return null;
			}
			string text2 = text.Substring(0, num);
			if (!string.IsNullOrEmpty(text2))
			{
				ConfigMgr.write<SIDInfo>(new SIDInfo
				{
					lastUpdateTime = Util.getNowMilliseconds(),
					strSid = text2
				});
			}
			return text2;
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			try
			{
				if ((!this.strCurrUrl.Contains("mail.qq.com/cgi-bin/readtemplate?") || !this.strCurrUrl.Contains("loc=today,,,10")) && !this.strOldUrl.Contains(this.strCurrUrl) && !(this.strOldUrl == this.strCurrUrl))
				{
					this.newMailDetail.InvokeScript("eval", new string[]
					{
						"history.go(-1)"
					});
					cancelEventArgs_0.set_Cancel(true);
					this.strOldUrl = this.strCurrUrl;
				}
				else
				{
					cancelEventArgs_0.set_Cancel(false);
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("QQMailNewMailView", ex.get_Message());
				cancelEventArgs_0.set_Cancel(false);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/QQMail/UI/QQMailNewMailView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.webview = (Grid)base.FindName("webview");
			this.newMailDetail = (WebBrowser)base.FindName("newMailDetail");
			this.canvasProgressBar = (Canvas)base.FindName("canvasProgressBar");
			this.ProgressBar = (PerformanceProgressBar)base.FindName("ProgressBar");
		}
	}
}
